/*
 * Copyright 2017 - 2024 the original author or authors.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see [https://www.gnu.org/licenses/]
 */

package infra.beans.factory.xml;

import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import infra.beans.BeanMetadataAttribute;
import infra.beans.BeanMetadataAttributeAccessor;
import infra.beans.PropertyValue;
import infra.beans.factory.config.BeanDefinition;
import infra.beans.factory.config.BeanDefinitionHolder;
import infra.beans.factory.config.ConstructorArgumentValues;
import infra.beans.factory.config.RuntimeBeanNameReference;
import infra.beans.factory.config.RuntimeBeanReference;
import infra.beans.factory.config.TypedStringValue;
import infra.beans.factory.parsing.BeanEntry;
import infra.beans.factory.parsing.ConstructorArgumentEntry;
import infra.beans.factory.parsing.ParseState;
import infra.beans.factory.parsing.ProblemReporter;
import infra.beans.factory.parsing.PropertyEntry;
import infra.beans.factory.parsing.QualifierEntry;
import infra.beans.factory.parsing.SourceExtractor;
import infra.beans.factory.support.AbstractBeanDefinition;
import infra.beans.factory.support.AutowireCandidateQualifier;
import infra.beans.factory.support.BeanDefinitionDefaults;
import infra.beans.factory.support.BeanDefinitionReaderUtils;
import infra.beans.factory.support.LookupOverride;
import infra.beans.factory.support.ManagedArray;
import infra.beans.factory.support.ManagedList;
import infra.beans.factory.support.ManagedMap;
import infra.beans.factory.support.ManagedProperties;
import infra.beans.factory.support.ManagedSet;
import infra.beans.factory.support.MethodOverrides;
import infra.beans.factory.support.ReplaceOverride;
import infra.lang.Assert;
import infra.lang.Nullable;
import infra.logging.Logger;
import infra.logging.LoggerFactory;
import infra.util.ClassUtils;
import infra.util.CollectionUtils;
import infra.util.ObjectUtils;
import infra.util.StringUtils;
import infra.util.xml.DomUtils;

/**
 * Stateful delegate class used to parse XML bean definitions.
 * Intended for use by both the main parser and any extension
 * {@link BeanDefinitionParser BeanDefinitionParsers} or
 * {@link BeanDefinitionDecorator BeanDefinitionDecorators}.
 *
 * @author Rob Harrop
 * @author Juergen Hoeller
 * @author Rod Johnson
 * @author Mark Fisher
 * @author Gary Russell
 * @see ParserContext
 * @see DefaultBeanDefinitionDocumentReader
 * @since 4.0
 */
public class BeanDefinitionParserDelegate {
  public static final Logger log = LoggerFactory.getLogger(BeanDefinitionParserDelegate.class);

  public static final String BEANS_NAMESPACE_URI = "http://www.springframework.org/schema/beans";

  public static final String MULTI_VALUE_ATTRIBUTE_DELIMITERS = ",; ";

  /**
   * Value of a T/F attribute that represents true.
   * Anything else represents false.
   */
  public static final String TRUE_VALUE = "true";

  public static final String FALSE_VALUE = "false";

  public static final String DEFAULT_VALUE = "default";

  public static final String DESCRIPTION_ELEMENT = "description";

  public static final String AUTOWIRE_NO_VALUE = "no";

  public static final String AUTOWIRE_BY_NAME_VALUE = "byName";

  public static final String AUTOWIRE_BY_TYPE_VALUE = "byType";

  public static final String AUTOWIRE_CONSTRUCTOR_VALUE = "constructor";

  public static final String AUTOWIRE_AUTODETECT_VALUE = "autodetect";

  public static final String NAME_ATTRIBUTE = "name";

  public static final String BEAN_ELEMENT = "bean";

  public static final String META_ELEMENT = "meta";

  public static final String ID_ATTRIBUTE = "id";

  public static final String PARENT_ATTRIBUTE = "parent";

  public static final String CLASS_ATTRIBUTE = "class";

  public static final String ABSTRACT_ATTRIBUTE = "abstract";

  public static final String SCOPE_ATTRIBUTE = "scope";

  private static final String SINGLETON_ATTRIBUTE = "singleton";

  public static final String LAZY_INIT_ATTRIBUTE = "lazy-init";

  public static final String AUTOWIRE_ATTRIBUTE = "autowire";

  public static final String AUTOWIRE_CANDIDATE_ATTRIBUTE = "autowire-candidate";

  public static final String PRIMARY_ATTRIBUTE = "primary";

  public static final String DEPENDS_ON_ATTRIBUTE = "depends-on";

  public static final String INIT_METHOD_ATTRIBUTE = "init-method";

  public static final String DESTROY_METHOD_ATTRIBUTE = "destroy-method";

  public static final String FACTORY_METHOD_ATTRIBUTE = "factory-method";

  public static final String FACTORY_BEAN_ATTRIBUTE = "factory-bean";

  public static final String CONSTRUCTOR_ARG_ELEMENT = "constructor-arg";

  public static final String INDEX_ATTRIBUTE = "index";

  public static final String TYPE_ATTRIBUTE = "type";

  public static final String VALUE_TYPE_ATTRIBUTE = "value-type";

  public static final String KEY_TYPE_ATTRIBUTE = "key-type";

  public static final String PROPERTY_ELEMENT = "property";

  public static final String REF_ATTRIBUTE = "ref";

  public static final String VALUE_ATTRIBUTE = "value";

  public static final String LOOKUP_METHOD_ELEMENT = "lookup-method";

  public static final String REPLACED_METHOD_ELEMENT = "replaced-method";

  public static final String REPLACER_ATTRIBUTE = "replacer";

  public static final String ARG_TYPE_ELEMENT = "arg-type";

  public static final String ARG_TYPE_MATCH_ATTRIBUTE = "match";

  public static final String REF_ELEMENT = "ref";

  public static final String IDREF_ELEMENT = "idref";

  public static final String BEAN_REF_ATTRIBUTE = "bean";

  public static final String PARENT_REF_ATTRIBUTE = "parent";

  public static final String VALUE_ELEMENT = "value";

  public static final String NULL_ELEMENT = "null";

  public static final String ARRAY_ELEMENT = "array";

  public static final String LIST_ELEMENT = "list";

  public static final String SET_ELEMENT = "set";

  public static final String MAP_ELEMENT = "map";

  public static final String ENTRY_ELEMENT = "entry";

  public static final String KEY_ELEMENT = "key";

  public static final String KEY_ATTRIBUTE = "key";

  public static final String KEY_REF_ATTRIBUTE = "key-ref";

  public static final String VALUE_REF_ATTRIBUTE = "value-ref";

  public static final String PROPS_ELEMENT = "props";

  public static final String PROP_ELEMENT = "prop";

  public static final String MERGE_ATTRIBUTE = "merge";

  public static final String QUALIFIER_ELEMENT = "qualifier";

  public static final String QUALIFIER_ATTRIBUTE_ELEMENT = "attribute";

  public static final String DEFAULT_LAZY_INIT_ATTRIBUTE = "default-lazy-init";

  public static final String DEFAULT_MERGE_ATTRIBUTE = "default-merge";

  public static final String DEFAULT_AUTOWIRE_ATTRIBUTE = "default-autowire";

  public static final String DEFAULT_AUTOWIRE_CANDIDATES_ATTRIBUTE = "default-autowire-candidates";

  public static final String DEFAULT_INIT_METHOD_ATTRIBUTE = "default-init-method";

  public static final String DEFAULT_DESTROY_METHOD_ATTRIBUTE = "default-destroy-method";

  private final XmlReaderContext readerContext;

  private final DocumentDefaultsDefinition defaults = new DocumentDefaultsDefinition();

  private final ParseState parseState = new ParseState();

  /**
   * Stores all used bean names so we can enforce uniqueness on a per
   * beans-element basis. Duplicate bean ids/names may not exist within the
   * same level of beans element nesting, but may be duplicated across levels.
   */
  private final HashSet<String> usedNames = new HashSet<>();

  /**
   * Create a new BeanDefinitionParserDelegate associated with the supplied
   * {@link XmlReaderContext}.
   */
  public BeanDefinitionParserDelegate(XmlReaderContext readerContext) {
    Assert.notNull(readerContext, "XmlReaderContext is required");
    this.readerContext = readerContext;
  }

  /**
   * Get the {@link XmlReaderContext} associated with this helper instance.
   */
  public final XmlReaderContext getReaderContext() {
    return this.readerContext;
  }

  /**
   * Invoke the {@link SourceExtractor}
   * to pull the source metadata from the supplied {@link Element}.
   */
  @Nullable
  protected Object extractSource(Element ele) {
    return this.readerContext.extractSource(ele);
  }

  /**
   * Report an error with the given message for the given source element.
   */
  protected void error(String message, Node source) {
    this.readerContext.error(message, source, this.parseState.snapshot());
  }

  /**
   * Report an error with the given message for the given source element.
   */
  protected void error(String message, Element source) {
    this.readerContext.error(message, source, this.parseState.snapshot());
  }

  /**
   * Report an error with the given message for the given source element.
   */
  protected void error(String message, Element source, Throwable cause) {
    this.readerContext.error(message, source, this.parseState.snapshot(), cause);
  }

  /**
   * Initialize the default settings assuming a {@code null} parent delegate.
   */
  public void initDefaults(Element root) {
    initDefaults(root, null);
  }

  /**
   * Initialize the default lazy-init, autowire, dependency check settings,
   * init-method, destroy-method and merge settings. Support nested 'beans'
   * element use cases by falling back to the given parent in case the
   * defaults are not explicitly set locally.
   *
   * @see #populateDefaults(DocumentDefaultsDefinition, DocumentDefaultsDefinition, Element)
   * @see #getDefaults()
   */
  public void initDefaults(Element root, @Nullable BeanDefinitionParserDelegate parent) {
    populateDefaults(this.defaults, (parent != null ? parent.defaults : null), root);
    this.readerContext.fireDefaultsRegistered(this.defaults);
  }

  /**
   * Populate the given DocumentDefaultsDefinition instance with the default lazy-init,
   * autowire, dependency check settings, init-method, destroy-method and merge settings.
   * Support nested 'beans' element use cases by falling back to {@code parentDefaults}
   * in case the defaults are not explicitly set locally.
   *
   * @param defaults the defaults to populate
   * @param parentDefaults the parent BeanDefinitionParserDelegate (if any) defaults to fall back to
   * @param root the root element of the current bean definition document (or nested beans element)
   */
  protected void populateDefaults(DocumentDefaultsDefinition defaults, @Nullable DocumentDefaultsDefinition parentDefaults, Element root) {
    String lazyInit = root.getAttribute(DEFAULT_LAZY_INIT_ATTRIBUTE);
    if (isDefaultValue(lazyInit)) {
      // Potentially inherited from outer <beans> sections, otherwise falling back to false.
      lazyInit = (parentDefaults != null ? parentDefaults.getLazyInit() : FALSE_VALUE);
    }
    defaults.setLazyInit(lazyInit);

    String merge = root.getAttribute(DEFAULT_MERGE_ATTRIBUTE);
    if (isDefaultValue(merge)) {
      // Potentially inherited from outer <beans> sections, otherwise falling back to false.
      merge = (parentDefaults != null ? parentDefaults.getMerge() : FALSE_VALUE);
    }
    defaults.setMerge(merge);

    String autowire = root.getAttribute(DEFAULT_AUTOWIRE_ATTRIBUTE);
    if (isDefaultValue(autowire)) {
      // Potentially inherited from outer <beans> sections, otherwise falling back to 'no'.
      autowire = (parentDefaults != null ? parentDefaults.getAutowire() : AUTOWIRE_NO_VALUE);
    }
    defaults.setAutowire(autowire);

    if (root.hasAttribute(DEFAULT_AUTOWIRE_CANDIDATES_ATTRIBUTE)) {
      defaults.setAutowireCandidates(root.getAttribute(DEFAULT_AUTOWIRE_CANDIDATES_ATTRIBUTE));
    }
    else if (parentDefaults != null) {
      defaults.setAutowireCandidates(parentDefaults.getAutowireCandidates());
    }

    if (root.hasAttribute(DEFAULT_INIT_METHOD_ATTRIBUTE)) {
      defaults.setInitMethod(root.getAttribute(DEFAULT_INIT_METHOD_ATTRIBUTE));
    }
    else if (parentDefaults != null) {
      defaults.setInitMethod(parentDefaults.getInitMethod());
    }

    if (root.hasAttribute(DEFAULT_DESTROY_METHOD_ATTRIBUTE)) {
      defaults.setDestroyMethod(root.getAttribute(DEFAULT_DESTROY_METHOD_ATTRIBUTE));
    }
    else if (parentDefaults != null) {
      defaults.setDestroyMethod(parentDefaults.getDestroyMethod());
    }

    defaults.setSource(this.readerContext.extractSource(root));
  }

  /**
   * Return the defaults definition object.
   */
  public DocumentDefaultsDefinition getDefaults() {
    return this.defaults;
  }

  /**
   * Return the default settings for bean definitions as indicated within
   * the attributes of the top-level {@code <beans/>} element.
   */
  public BeanDefinitionDefaults getBeanDefinitionDefaults() {
    BeanDefinitionDefaults bdd = new BeanDefinitionDefaults();
    bdd.setLazyInit(TRUE_VALUE.equalsIgnoreCase(this.defaults.getLazyInit()));
    bdd.setAutowireMode(getAutowireMode(DEFAULT_VALUE));
    bdd.setInitMethodName(this.defaults.getInitMethod());
    bdd.setDestroyMethodName(this.defaults.getDestroyMethod());
    return bdd;
  }

  /**
   * Return any patterns provided in the 'default-autowire-candidates'
   * attribute of the top-level {@code <beans/>} element.
   */
  @Nullable
  public String[] getAutowireCandidatePatterns() {
    String candidatePattern = this.defaults.getAutowireCandidates();
    return candidatePattern != null ? StringUtils.commaDelimitedListToStringArray(candidatePattern) : null;
  }

  /**
   * Parses the supplied {@code <bean>} element. May return {@code null}
   * if there were errors during parse. Errors are reported to the
   * {@link ProblemReporter}.
   */
  @Nullable
  public BeanDefinitionHolder parseBeanDefinitionElement(Element ele) {
    return parseBeanDefinitionElement(ele, null);
  }

  /**
   * Parses the supplied {@code <bean>} element. May return {@code null}
   * if there were errors during parse. Errors are reported to the
   * {@link ProblemReporter}.
   */
  @Nullable
  public BeanDefinitionHolder parseBeanDefinitionElement(Element ele, @Nullable BeanDefinition containingBean) {
    String id = ele.getAttribute(ID_ATTRIBUTE);
    String nameAttr = ele.getAttribute(NAME_ATTRIBUTE);

    ArrayList<String> aliases = new ArrayList<>();
    if (StringUtils.isNotEmpty(nameAttr)) {
      String[] nameArr = StringUtils.tokenizeToStringArray(nameAttr, MULTI_VALUE_ATTRIBUTE_DELIMITERS);
      CollectionUtils.addAll(aliases, nameArr);
    }

    String beanName = id;
    if (StringUtils.isBlank(beanName) && !aliases.isEmpty()) {
      beanName = aliases.remove(0);
      if (log.isTraceEnabled()) {
        log.trace("No XML 'id' specified - using '{}' as bean name and {} as aliases", beanName, aliases);
      }
    }

    if (containingBean == null) {
      checkNameUniqueness(beanName, aliases, ele);
    }

    BeanDefinition beanDefinition = parseBeanDefinitionElement(ele, beanName, containingBean);
    if (beanDefinition != null) {
      if (StringUtils.isBlank(beanName)) {
        try {
          if (containingBean != null) {
            beanName = BeanDefinitionReaderUtils.generateBeanName(
                    beanDefinition, this.readerContext.getRegistry(), true);
          }
          else {
            beanName = this.readerContext.generateBeanName(beanDefinition);
            // Register an alias for the plain bean class name, if still possible,
            // if the generator returned the class name plus a suffix.
            // This is expected for backwards compatibility.
            String beanClassName = beanDefinition.getBeanClassName();
            if (beanClassName != null
                    && beanName.startsWith(beanClassName)
                    && beanName.length() > beanClassName.length()
                    && !this.readerContext.getRegistry().isBeanNameInUse(beanClassName)) {
              aliases.add(beanClassName);
            }
          }
          if (log.isTraceEnabled()) {
            log.trace("Neither XML 'id' nor 'name' specified - using generated bean name [{}]", beanName);
          }
        }
        catch (Exception ex) {
          error(ex.getMessage(), ele);
          return null;
        }
      }
      String[] aliasesArray = StringUtils.toStringArray(aliases);
      return new BeanDefinitionHolder(beanDefinition, beanName, aliasesArray);
    }

    return null;
  }

  /**
   * Validate that the specified bean name and aliases have not been used already
   * within the current level of beans element nesting.
   */
  protected void checkNameUniqueness(String beanName, List<String> aliases, Element beanElement) {
    String foundName = null;

    if (StringUtils.hasText(beanName) && this.usedNames.contains(beanName)) {
      foundName = beanName;
    }
    if (foundName == null) {
      foundName = CollectionUtils.findFirstMatch(this.usedNames, aliases);
    }
    if (foundName != null) {
      error("Bean name '" + foundName + "' is already used in this <beans> element", beanElement);
    }

    this.usedNames.add(beanName);
    this.usedNames.addAll(aliases);
  }

  /**
   * Parse the bean definition itself, without regard to name or aliases. May return
   * {@code null} if problems occurred during the parsing of the bean definition.
   */
  @Nullable
  public BeanDefinition parseBeanDefinitionElement(
          Element ele, String beanName, @Nullable BeanDefinition containingBean) {

    this.parseState.push(new BeanEntry(beanName));

    String className = null;
    if (ele.hasAttribute(CLASS_ATTRIBUTE)) {
      className = ele.getAttribute(CLASS_ATTRIBUTE).trim();
    }
    String parent = null;
    if (ele.hasAttribute(PARENT_ATTRIBUTE)) {
      parent = ele.getAttribute(PARENT_ATTRIBUTE);
    }

    try {
      AbstractBeanDefinition bd = createBeanDefinition(className, parent);

      parseBeanDefinitionAttributes(ele, beanName, containingBean, bd);
      bd.setDescription(DomUtils.getChildElementValueByTagName(ele, DESCRIPTION_ELEMENT));

      parseMetaElements(ele, bd);
      parseLookupOverrideSubElements(ele, bd.getMethodOverrides());
      parseReplacedMethodSubElements(ele, bd.getMethodOverrides());

      parseConstructorArgElements(ele, bd);
      parsePropertyElements(ele, bd);
      parseQualifierElements(ele, bd);

      bd.setResource(this.readerContext.getResource());
      bd.setSource(extractSource(ele));

      return bd;
    }
    catch (ClassNotFoundException ex) {
      error("Bean class [" + className + "] not found", ele, ex);
    }
    catch (NoClassDefFoundError err) {
      error("Class that bean class [" + className + "] depends on not found", ele, err);
    }
    catch (Throwable ex) {
      error("Unexpected failure during bean definition parsing", ele, ex);
    }
    finally {
      this.parseState.pop();
    }

    return null;
  }

  /**
   * Apply the attributes of the given bean element to the given bean * definition.
   *
   * @param ele bean declaration element
   * @param beanName bean name
   * @param containingBean containing bean definition
   * @return a bean definition initialized according to the bean element attributes
   */
  public BeanDefinition parseBeanDefinitionAttributes(Element ele, String beanName,
          @Nullable BeanDefinition containingBean, AbstractBeanDefinition bd) {

    if (ele.hasAttribute(SINGLETON_ATTRIBUTE)) {
      error("Old 1.x 'singleton' attribute in use - upgrade to 'scope' declaration", ele);
    }
    else if (ele.hasAttribute(SCOPE_ATTRIBUTE)) {
      bd.setScope(ele.getAttribute(SCOPE_ATTRIBUTE));
    }
    else if (containingBean != null) {
      // Take default from containing bean in case of an inner bean definition.
      bd.setScope(containingBean.getScope());
    }

    if (ele.hasAttribute(ABSTRACT_ATTRIBUTE)) {
      bd.setAbstract(TRUE_VALUE.equals(ele.getAttribute(ABSTRACT_ATTRIBUTE)));
    }

    String lazyInit = ele.getAttribute(LAZY_INIT_ATTRIBUTE);
    if (isDefaultValue(lazyInit)) {
      lazyInit = this.defaults.getLazyInit();
    }
    bd.setLazyInit(TRUE_VALUE.equals(lazyInit));

    String autowire = ele.getAttribute(AUTOWIRE_ATTRIBUTE);
    bd.setAutowireMode(getAutowireMode(autowire));

    if (ele.hasAttribute(DEPENDS_ON_ATTRIBUTE)) {
      String dependsOn = ele.getAttribute(DEPENDS_ON_ATTRIBUTE);
      bd.setDependsOn(StringUtils.tokenizeToStringArray(dependsOn, MULTI_VALUE_ATTRIBUTE_DELIMITERS));
    }

    String autowireCandidate = ele.getAttribute(AUTOWIRE_CANDIDATE_ATTRIBUTE);
    if (isDefaultValue(autowireCandidate)) {
      String candidatePattern = this.defaults.getAutowireCandidates();
      if (candidatePattern != null) {
        String[] patterns = StringUtils.commaDelimitedListToStringArray(candidatePattern);
        bd.setAutowireCandidate(StringUtils.simpleMatch(patterns, beanName));
      }
    }
    else {
      bd.setAutowireCandidate(TRUE_VALUE.equals(autowireCandidate));
    }

    if (ele.hasAttribute(PRIMARY_ATTRIBUTE)) {
      bd.setPrimary(TRUE_VALUE.equals(ele.getAttribute(PRIMARY_ATTRIBUTE)));
    }

    if (ele.hasAttribute(INIT_METHOD_ATTRIBUTE)) {
      // @since 4.0
      String initMethodName = ele.getAttribute(INIT_METHOD_ATTRIBUTE);
      String[] initMethods = StringUtils.tokenizeToStringArray(initMethodName, MULTI_VALUE_ATTRIBUTE_DELIMITERS);
      bd.setInitMethodNames(initMethods);
    }
    else if (this.defaults.getInitMethod() != null) {
      String[] initMethods = StringUtils.tokenizeToStringArray(defaults.getInitMethod(), MULTI_VALUE_ATTRIBUTE_DELIMITERS);
      bd.setInitMethodNames(initMethods);
      bd.setEnforceInitMethod(false);
    }

    if (ele.hasAttribute(DESTROY_METHOD_ATTRIBUTE)) {
      String destroyMethodName = ele.getAttribute(DESTROY_METHOD_ATTRIBUTE);
      bd.setDestroyMethodNames(destroyMethodName);
    }
    else if (this.defaults.getDestroyMethod() != null) {
      bd.setDestroyMethodName(this.defaults.getDestroyMethod());
      bd.setEnforceDestroyMethod(false);
    }

    if (ele.hasAttribute(FACTORY_METHOD_ATTRIBUTE)) {
      bd.setFactoryMethodName(ele.getAttribute(FACTORY_METHOD_ATTRIBUTE));
    }
    if (ele.hasAttribute(FACTORY_BEAN_ATTRIBUTE)) {
      bd.setFactoryBeanName(ele.getAttribute(FACTORY_BEAN_ATTRIBUTE));
    }

    return bd;
  }

  /**
   * Create a bean definition for the given class name and parent name.
   *
   * @param className the name of the bean class
   * @param parentName the name of the bean's parent bean
   * @return the newly created bean definition
   * @throws ClassNotFoundException if bean class resolution was attempted but failed
   */
  protected AbstractBeanDefinition createBeanDefinition(@Nullable String className, @Nullable String parentName)
          throws ClassNotFoundException {

    return BeanDefinitionReaderUtils.createBeanDefinition(
            parentName, className, readerContext.getBeanClassLoader());
  }

  /**
   * Parse the meta elements underneath the given element, if any.
   */
  public void parseMetaElements(Element ele, BeanMetadataAttributeAccessor attributeAccessor) {
    NodeList nl = ele.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
      Node node = nl.item(i);
      if (isCandidateElement(node) && nodeNameEquals(node, META_ELEMENT)) {
        Element metaElement = (Element) node;
        String key = metaElement.getAttribute(KEY_ATTRIBUTE);
        String value = metaElement.getAttribute(VALUE_ATTRIBUTE);
        BeanMetadataAttribute attribute = new BeanMetadataAttribute(key, value);
        attribute.setSource(extractSource(metaElement));
        attributeAccessor.addMetadataAttribute(attribute);
      }
    }
  }

  /**
   * Parse the given autowire attribute value into
   * {@link BeanDefinition} autowire constants.
   */
  public int getAutowireMode(String attrValue) {
    String attr = attrValue;
    if (isDefaultValue(attr)) {
      attr = this.defaults.getAutowire();
    }
    int autowire = AbstractBeanDefinition.AUTOWIRE_NO;
    if (AUTOWIRE_BY_NAME_VALUE.equals(attr)) {
      autowire = AbstractBeanDefinition.AUTOWIRE_BY_NAME;
    }
    else if (AUTOWIRE_BY_TYPE_VALUE.equals(attr)) {
      autowire = AbstractBeanDefinition.AUTOWIRE_BY_TYPE;
    }
    else if (AUTOWIRE_CONSTRUCTOR_VALUE.equals(attr)) {
      autowire = AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR;
    }
    else if (AUTOWIRE_AUTODETECT_VALUE.equals(attr)) {
      autowire = AbstractBeanDefinition.AUTOWIRE_AUTODETECT;
    }
    // Else leave default value.
    return autowire;
  }

  /**
   * Parse constructor-arg sub-elements of the given bean element.
   */
  public void parseConstructorArgElements(Element beanEle, BeanDefinition bd) {
    NodeList nl = beanEle.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
      Node node = nl.item(i);
      if (isCandidateElement(node) && nodeNameEquals(node, CONSTRUCTOR_ARG_ELEMENT)) {
        parseConstructorArgElement((Element) node, bd);
      }
    }
  }

  /**
   * Parse property sub-elements of the given bean element.
   */
  public void parsePropertyElements(Element beanEle, BeanDefinition bd) {
    NodeList nl = beanEle.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
      Node node = nl.item(i);
      if (isCandidateElement(node) && nodeNameEquals(node, PROPERTY_ELEMENT)) {
        parsePropertyElement((Element) node, bd);
      }
    }
  }

  /**
   * Parse qualifier sub-elements of the given bean element.
   */
  public void parseQualifierElements(Element beanEle, AbstractBeanDefinition bd) {
    NodeList nl = beanEle.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
      Node node = nl.item(i);
      if (isCandidateElement(node) && nodeNameEquals(node, QUALIFIER_ELEMENT)) {
        parseQualifierElement((Element) node, bd);
      }
    }
  }

  /**
   * Parse lookup-override sub-elements of the given bean element.
   */
  public void parseLookupOverrideSubElements(Element beanEle, MethodOverrides overrides) {
    NodeList nl = beanEle.getChildNodes();
    int length = nl.getLength();
    for (int i = 0; i < length; i++) {
      Node node = nl.item(i);
      if (isCandidateElement(node) && nodeNameEquals(node, LOOKUP_METHOD_ELEMENT)) {
        Element ele = (Element) node;
        String methodName = ele.getAttribute(NAME_ATTRIBUTE);
        String beanRef = ele.getAttribute(BEAN_ELEMENT);
        LookupOverride override = new LookupOverride(methodName, beanRef);
        override.setSource(extractSource(ele));
        overrides.addOverride(override);
      }
    }
  }

  /**
   * Parse replaced-method sub-elements of the given bean element.
   */
  public void parseReplacedMethodSubElements(Element beanEle, MethodOverrides overrides) {
    NodeList nl = beanEle.getChildNodes();
    int length = nl.getLength();
    for (int i = 0; i < length; i++) {
      Node node = nl.item(i);
      if (isCandidateElement(node) && nodeNameEquals(node, REPLACED_METHOD_ELEMENT)) {
        Element replacedMethodEle = (Element) node;
        String name = replacedMethodEle.getAttribute(NAME_ATTRIBUTE);
        String callback = replacedMethodEle.getAttribute(REPLACER_ATTRIBUTE);
        ReplaceOverride replaceOverride = new ReplaceOverride(name, callback);
        // Look for arg-type match elements.
        List<Element> argTypeEles = DomUtils.getChildElementsByTagName(replacedMethodEle, ARG_TYPE_ELEMENT);
        for (Element argTypeEle : argTypeEles) {
          String match = argTypeEle.getAttribute(ARG_TYPE_MATCH_ATTRIBUTE);
          match = (StringUtils.hasText(match) ? match : DomUtils.getTextValue(argTypeEle));
          if (StringUtils.hasText(match)) {
            replaceOverride.addTypeIdentifier(match);
          }
        }
        replaceOverride.setSource(extractSource(replacedMethodEle));
        overrides.addOverride(replaceOverride);
      }
    }
  }

  /**
   * Parse a constructor-arg element.
   */
  public void parseConstructorArgElement(Element ele, BeanDefinition bd) {
    String indexAttr = ele.getAttribute(INDEX_ATTRIBUTE);
    String typeAttr = ele.getAttribute(TYPE_ATTRIBUTE);
    String nameAttr = ele.getAttribute(NAME_ATTRIBUTE);
    if (StringUtils.isNotEmpty(indexAttr)) {
      try {
        int index = Integer.parseInt(indexAttr);
        if (index < 0) {
          error("'index' cannot be lower than 0", ele);
        }
        else {
          try {
            this.parseState.push(new ConstructorArgumentEntry(index));
            Object value = parsePropertyValue(ele, bd, null);
            ConstructorArgumentValues.ValueHolder valueHolder = new ConstructorArgumentValues.ValueHolder(value);
            if (StringUtils.isNotEmpty(typeAttr)) {
              valueHolder.setType(typeAttr);
            }
            if (StringUtils.isNotEmpty(nameAttr)) {
              valueHolder.setName(nameAttr);
            }
            valueHolder.setSource(extractSource(ele));
            if (bd.getConstructorArgumentValues().hasIndexedArgumentValue(index)) {
              error("Ambiguous constructor-arg entries for index " + index, ele);
            }
            else {
              bd.getConstructorArgumentValues().addIndexedArgumentValue(index, valueHolder);
            }
          }
          finally {
            this.parseState.pop();
          }
        }
      }
      catch (NumberFormatException ex) {
        error("Attribute 'index' of tag 'constructor-arg' must be an integer", ele);
      }
    }
    else {
      try {
        this.parseState.push(new ConstructorArgumentEntry());
        Object value = parsePropertyValue(ele, bd, null);
        ConstructorArgumentValues.ValueHolder valueHolder = new ConstructorArgumentValues.ValueHolder(value);
        if (StringUtils.isNotEmpty(typeAttr)) {
          valueHolder.setType(typeAttr);
        }
        if (StringUtils.isNotEmpty(nameAttr)) {
          valueHolder.setName(nameAttr);
        }
        valueHolder.setSource(extractSource(ele));
        bd.getConstructorArgumentValues().addGenericArgumentValue(valueHolder);
      }
      finally {
        this.parseState.pop();
      }
    }
  }

  /**
   * Parse a property element.
   */
  public void parsePropertyElement(Element ele, BeanDefinition bd) {
    String propertyName = ele.getAttribute(NAME_ATTRIBUTE);
    if (StringUtils.isEmpty(propertyName)) {
      error("Tag 'property' must have a 'name' attribute", ele);
      return;
    }
    this.parseState.push(new PropertyEntry(propertyName));
    try {
      if (bd.getPropertyValues().contains(propertyName)) {
        error("Multiple 'property' definitions for property '" + propertyName + "'", ele);
        return;
      }
      Object val = parsePropertyValue(ele, bd, propertyName);
      PropertyValue pv = new PropertyValue(propertyName, val);
      parseMetaElements(ele, pv);
      pv.setSource(extractSource(ele));
      bd.getPropertyValues().add(pv);
    }
    finally {
      this.parseState.pop();
    }
  }

  /**
   * Parse a qualifier element.
   */
  public void parseQualifierElement(Element ele, AbstractBeanDefinition bd) {
    String typeName = ele.getAttribute(TYPE_ATTRIBUTE);
    if (StringUtils.isEmpty(typeName)) {
      error("Tag 'qualifier' must have a 'type' attribute", ele);
      return;
    }
    this.parseState.push(new QualifierEntry(typeName));
    try {
      AutowireCandidateQualifier qualifier = new AutowireCandidateQualifier(typeName);
      qualifier.setSource(extractSource(ele));
      String value = ele.getAttribute(VALUE_ATTRIBUTE);
      if (StringUtils.isNotEmpty(value)) {
        qualifier.setAttribute(AutowireCandidateQualifier.VALUE_KEY, value);
      }
      NodeList nl = ele.getChildNodes();
      int length = nl.getLength();
      for (int i = 0; i < length; i++) {
        Node node = nl.item(i);
        if (isCandidateElement(node) && nodeNameEquals(node, QUALIFIER_ATTRIBUTE_ELEMENT)) {
          Element attributeEle = (Element) node;
          String attributeName = attributeEle.getAttribute(KEY_ATTRIBUTE);
          String attributeValue = attributeEle.getAttribute(VALUE_ATTRIBUTE);
          if (StringUtils.isNotEmpty(attributeName) && StringUtils.isNotEmpty(attributeValue)) {
            BeanMetadataAttribute attribute = new BeanMetadataAttribute(attributeName, attributeValue);
            attribute.setSource(extractSource(attributeEle));
            qualifier.addMetadataAttribute(attribute);
          }
          else {
            error("Qualifier 'attribute' tag must have a 'key' and 'value'", attributeEle);
            return;
          }
        }
      }
      bd.addQualifier(qualifier);
    }
    finally {
      this.parseState.pop();
    }
  }

  /**
   * Get the value of a property element. May be a list etc.
   * Also used for constructor arguments, "propertyName" being null in this case.
   */
  @Nullable
  public Object parsePropertyValue(Element ele, BeanDefinition bd, @Nullable String propertyName) {
    String elementName = (propertyName != null ?
            "<property> element for property '" + propertyName + "'" :
            "<constructor-arg> element");

    // Should only have one child element: ref, value, list, etc.
    NodeList nl = ele.getChildNodes();
    Element subElement = null;
    int length = nl.getLength();
    for (int i = 0; i < length; i++) {
      Node node = nl.item(i);
      if (node instanceof Element && !nodeNameEquals(node, DESCRIPTION_ELEMENT) &&
              !nodeNameEquals(node, META_ELEMENT)) {
        // Child element is what we're looking for.
        if (subElement != null) {
          error(elementName + " must not contain more than one sub-element", ele);
        }
        else {
          subElement = (Element) node;
        }
      }
    }

    boolean hasRefAttribute = ele.hasAttribute(REF_ATTRIBUTE);
    boolean hasValueAttribute = ele.hasAttribute(VALUE_ATTRIBUTE);
    if ((hasRefAttribute && hasValueAttribute) ||
            ((hasRefAttribute || hasValueAttribute) && subElement != null)) {
      error(elementName +
              " is only allowed to contain either 'ref' attribute OR 'value' attribute OR sub-element", ele);
    }

    if (hasRefAttribute) {
      String refName = ele.getAttribute(REF_ATTRIBUTE);
      if (StringUtils.isBlank(refName)) {
        error(elementName + " contains empty 'ref' attribute", ele);
      }
      RuntimeBeanReference ref = new RuntimeBeanReference(refName);
      ref.setSource(extractSource(ele));
      return ref;
    }
    else if (hasValueAttribute) {
      TypedStringValue valueHolder = new TypedStringValue(ele.getAttribute(VALUE_ATTRIBUTE));
      valueHolder.setSource(extractSource(ele));
      return valueHolder;
    }
    else if (subElement != null) {
      return parsePropertySubElement(subElement, bd);
    }
    else {
      // Neither child element nor "ref" or "value" attribute found.
      error(elementName + " must specify a ref or value", ele);
      return null;
    }
  }

  /**
   * Parse a value, ref or collection sub-element of a property or
   * constructor-arg element.
   *
   * @param ele subelement of property element; we don't know which yet
   * @param bd the current bean definition (if any)
   */
  @Nullable
  public Object parsePropertySubElement(Element ele, @Nullable BeanDefinition bd) {
    return parsePropertySubElement(ele, bd, null);
  }

  /**
   * Parse a value, ref or collection sub-element of a property or
   * constructor-arg element.
   *
   * @param ele subelement of property element; we don't know which yet
   * @param bd the current bean definition (if any)
   * @param defaultValueType the default type (class name) for any
   * {@code <value>} tag that might be created
   */
  @Nullable
  public Object parsePropertySubElement(Element ele, @Nullable BeanDefinition bd, @Nullable String defaultValueType) {
    if (!isDefaultNamespace(ele)) {
      return parseNestedCustomElement(ele, bd);
    }
    else if (nodeNameEquals(ele, BEAN_ELEMENT)) {
      BeanDefinitionHolder nestedBd = parseBeanDefinitionElement(ele, bd);
      if (nestedBd != null) {
        nestedBd = decorateBeanDefinitionIfRequired(ele, nestedBd, bd);
      }
      return nestedBd;
    }
    else if (nodeNameEquals(ele, REF_ELEMENT)) {
      // A generic reference to any name of any bean.
      String refName = ele.getAttribute(BEAN_REF_ATTRIBUTE);
      boolean toParent = false;
      if (StringUtils.isEmpty(refName)) {
        // A reference to the id of another bean in a parent context.
        refName = ele.getAttribute(PARENT_REF_ATTRIBUTE);
        toParent = true;
        if (StringUtils.isEmpty(refName)) {
          error("'bean' or 'parent' is required for <ref> element", ele);
          return null;
        }
      }
      if (StringUtils.isBlank(refName)) {
        error("<ref> element contains empty target attribute", ele);
        return null;
      }
      RuntimeBeanReference ref = new RuntimeBeanReference(refName, toParent);
      ref.setSource(extractSource(ele));
      return ref;
    }
    else if (nodeNameEquals(ele, IDREF_ELEMENT)) {
      return parseIdRefElement(ele);
    }
    else if (nodeNameEquals(ele, VALUE_ELEMENT)) {
      return parseValueElement(ele, defaultValueType);
    }
    else if (nodeNameEquals(ele, NULL_ELEMENT)) {
      // It's a distinguished null value. Let's wrap it in a TypedStringValue
      // object in order to preserve the source location.
      TypedStringValue nullHolder = new TypedStringValue(null);
      nullHolder.setSource(extractSource(ele));
      return nullHolder;
    }
    else if (nodeNameEquals(ele, ARRAY_ELEMENT)) {
      return parseArrayElement(ele, bd);
    }
    else if (nodeNameEquals(ele, LIST_ELEMENT)) {
      return parseListElement(ele, bd);
    }
    else if (nodeNameEquals(ele, SET_ELEMENT)) {
      return parseSetElement(ele, bd);
    }
    else if (nodeNameEquals(ele, MAP_ELEMENT)) {
      return parseMapElement(ele, bd);
    }
    else if (nodeNameEquals(ele, PROPS_ELEMENT)) {
      return parsePropsElement(ele);
    }
    else {
      error("Unknown property sub-element: [" + ele.getNodeName() + "]", ele);
      return null;
    }
  }

  /**
   * Return a typed String value Object for the given 'idref' element.
   */
  @Nullable
  public Object parseIdRefElement(Element ele) {
    // A generic reference to any name of any bean.
    String refName = ele.getAttribute(BEAN_REF_ATTRIBUTE);
    if (StringUtils.isEmpty(refName)) {
      error("'bean' is required for <idref> element", ele);
      return null;
    }
    if (StringUtils.isBlank(refName)) {
      error("<idref> element contains empty target attribute", ele);
      return null;
    }
    RuntimeBeanNameReference ref = new RuntimeBeanNameReference(refName);
    ref.setSource(extractSource(ele));
    return ref;
  }

  /**
   * Return a typed String value Object for the given value element.
   */
  public Object parseValueElement(Element ele, @Nullable String defaultTypeName) {
    // It's a literal value.
    String value = DomUtils.getTextValue(ele);
    String specifiedTypeName = ele.getAttribute(TYPE_ATTRIBUTE);
    String typeName = specifiedTypeName;
    if (StringUtils.isBlank(typeName)) {
      typeName = defaultTypeName;
    }
    try {
      TypedStringValue typedValue = buildTypedStringValue(value, typeName);
      typedValue.setSource(extractSource(ele));
      typedValue.setSpecifiedTypeName(specifiedTypeName);
      return typedValue;
    }
    catch (ClassNotFoundException ex) {
      error("Type class [" + typeName + "] not found for <value> element", ele, ex);
      return value;
    }
  }

  /**
   * Build a typed String value Object for the given raw value.
   *
   * @see TypedStringValue
   */
  protected TypedStringValue buildTypedStringValue(String value, @Nullable String targetTypeName)
          throws ClassNotFoundException {

    ClassLoader classLoader = this.readerContext.getBeanClassLoader();
    TypedStringValue typedValue;
    if (StringUtils.isBlank(targetTypeName)) {
      typedValue = new TypedStringValue(value);
    }
    else if (classLoader != null) {
      Class<?> targetType = ClassUtils.forName(targetTypeName, classLoader);
      typedValue = new TypedStringValue(value, targetType);
    }
    else {
      typedValue = new TypedStringValue(value, targetTypeName);
    }
    return typedValue;
  }

  /**
   * Parse an array element.
   */
  public Object parseArrayElement(Element arrayEle, @Nullable BeanDefinition bd) {
    String elementType = arrayEle.getAttribute(VALUE_TYPE_ATTRIBUTE);
    NodeList nl = arrayEle.getChildNodes();
    ManagedArray target = new ManagedArray(elementType, nl.getLength());
    target.setSource(extractSource(arrayEle));
    target.setElementTypeName(elementType);
    target.setMergeEnabled(parseMergeAttribute(arrayEle));
    parseCollectionElements(nl, target, bd, elementType);
    return target;
  }

  /**
   * Parse a list element.
   */
  public List<Object> parseListElement(Element collectionEle, @Nullable BeanDefinition bd) {
    String defaultElementType = collectionEle.getAttribute(VALUE_TYPE_ATTRIBUTE);
    NodeList nl = collectionEle.getChildNodes();
    ManagedList<Object> target = new ManagedList<>(nl.getLength());
    target.setSource(extractSource(collectionEle));
    target.setElementTypeName(defaultElementType);
    target.setMergeEnabled(parseMergeAttribute(collectionEle));
    parseCollectionElements(nl, target, bd, defaultElementType);
    return target;
  }

  /**
   * Parse a set element.
   */
  public Set<Object> parseSetElement(Element collectionEle, @Nullable BeanDefinition bd) {
    String defaultElementType = collectionEle.getAttribute(VALUE_TYPE_ATTRIBUTE);
    NodeList nl = collectionEle.getChildNodes();
    ManagedSet<Object> target = new ManagedSet<>(nl.getLength());
    target.setSource(extractSource(collectionEle));
    target.setElementTypeName(defaultElementType);
    target.setMergeEnabled(parseMergeAttribute(collectionEle));
    parseCollectionElements(nl, target, bd, defaultElementType);
    return target;
  }

  protected void parseCollectionElements(
          NodeList elementNodes, Collection<Object> target, @Nullable BeanDefinition bd, String defaultElementType) {

    for (int i = 0; i < elementNodes.getLength(); i++) {
      Node node = elementNodes.item(i);
      if (node instanceof Element && !nodeNameEquals(node, DESCRIPTION_ELEMENT)) {
        target.add(parsePropertySubElement((Element) node, bd, defaultElementType));
      }
    }
  }

  /**
   * Parse a map element.
   */
  public Map<Object, Object> parseMapElement(Element mapEle, @Nullable BeanDefinition bd) {
    String defaultKeyType = mapEle.getAttribute(KEY_TYPE_ATTRIBUTE);
    String defaultValueType = mapEle.getAttribute(VALUE_TYPE_ATTRIBUTE);

    List<Element> entryEles = DomUtils.getChildElementsByTagName(mapEle, ENTRY_ELEMENT);
    ManagedMap<Object, Object> map = new ManagedMap<>(entryEles.size());
    map.setSource(extractSource(mapEle));
    map.setKeyTypeName(defaultKeyType);
    map.setValueTypeName(defaultValueType);
    map.setMergeEnabled(parseMergeAttribute(mapEle));

    for (Element entryEle : entryEles) {
      // Should only have one value child element: ref, value, list, etc.
      // Optionally, there might be a key child element.
      NodeList entrySubNodes = entryEle.getChildNodes();
      Element keyEle = null;
      Element valueEle = null;
      for (int j = 0; j < entrySubNodes.getLength(); j++) {
        Node node = entrySubNodes.item(j);
        if (node instanceof Element candidateEle) {
          if (nodeNameEquals(candidateEle, KEY_ELEMENT)) {
            if (keyEle != null) {
              error("<entry> element is only allowed to contain one <key> sub-element", entryEle);
            }
            else {
              keyEle = candidateEle;
            }
          }
          else {
            // Child element is what we're looking for.
            if (nodeNameEquals(candidateEle, DESCRIPTION_ELEMENT)) {
              // the element is a <description> -> ignore it
            }
            else if (valueEle != null) {
              error("<entry> element must not contain more than one value sub-element", entryEle);
            }
            else {
              valueEle = candidateEle;
            }
          }
        }
      }

      // Extract key from attribute or sub-element.
      Object key = null;
      boolean hasKeyAttribute = entryEle.hasAttribute(KEY_ATTRIBUTE);
      boolean hasKeyRefAttribute = entryEle.hasAttribute(KEY_REF_ATTRIBUTE);
      if ((hasKeyAttribute && hasKeyRefAttribute) ||
              (hasKeyAttribute || hasKeyRefAttribute) && keyEle != null) {
        error("<entry> element is only allowed to contain either " +
                "a 'key' attribute OR a 'key-ref' attribute OR a <key> sub-element", entryEle);
      }
      if (hasKeyAttribute) {
        key = buildTypedStringValueForMap(entryEle.getAttribute(KEY_ATTRIBUTE), defaultKeyType, entryEle);
      }
      else if (hasKeyRefAttribute) {
        String refName = entryEle.getAttribute(KEY_REF_ATTRIBUTE);
        if (StringUtils.isBlank(refName)) {
          error("<entry> element contains empty 'key-ref' attribute", entryEle);
        }
        RuntimeBeanReference ref = new RuntimeBeanReference(refName);
        ref.setSource(extractSource(entryEle));
        key = ref;
      }
      else if (keyEle != null) {
        key = parseKeyElement(keyEle, bd, defaultKeyType);
      }
      else {
        error("<entry> element must specify a key", entryEle);
      }

      // Extract value from attribute or sub-element.
      Object value = null;
      boolean hasValueAttribute = entryEle.hasAttribute(VALUE_ATTRIBUTE);
      boolean hasValueRefAttribute = entryEle.hasAttribute(VALUE_REF_ATTRIBUTE);
      boolean hasValueTypeAttribute = entryEle.hasAttribute(VALUE_TYPE_ATTRIBUTE);
      if ((hasValueAttribute && hasValueRefAttribute) ||
              (hasValueAttribute || hasValueRefAttribute) && valueEle != null) {
        error("<entry> element is only allowed to contain either " +
                "'value' attribute OR 'value-ref' attribute OR <value> sub-element", entryEle);
      }
      if ((hasValueTypeAttribute && hasValueRefAttribute) ||
              (hasValueTypeAttribute && !hasValueAttribute) ||
              (hasValueTypeAttribute && valueEle != null)) {
        error("<entry> element is only allowed to contain a 'value-type' " +
                "attribute when it has a 'value' attribute", entryEle);
      }
      if (hasValueAttribute) {
        String valueType = entryEle.getAttribute(VALUE_TYPE_ATTRIBUTE);
        if (StringUtils.isBlank(valueType)) {
          valueType = defaultValueType;
        }
        value = buildTypedStringValueForMap(entryEle.getAttribute(VALUE_ATTRIBUTE), valueType, entryEle);
      }
      else if (hasValueRefAttribute) {
        String refName = entryEle.getAttribute(VALUE_REF_ATTRIBUTE);
        if (StringUtils.isBlank(refName)) {
          error("<entry> element contains empty 'value-ref' attribute", entryEle);
        }
        RuntimeBeanReference ref = new RuntimeBeanReference(refName);
        ref.setSource(extractSource(entryEle));
        value = ref;
      }
      else if (valueEle != null) {
        value = parsePropertySubElement(valueEle, bd, defaultValueType);
      }
      else {
        error("<entry> element must specify a value", entryEle);
      }

      // Add final key and value to the Map.
      map.put(key, value);
    }

    return map;
  }

  /**
   * Build a typed String value Object for the given raw value.
   *
   * @see TypedStringValue
   */
  protected final Object buildTypedStringValueForMap(String value, String defaultTypeName, Element entryEle) {
    try {
      TypedStringValue typedValue = buildTypedStringValue(value, defaultTypeName);
      typedValue.setSource(extractSource(entryEle));
      return typedValue;
    }
    catch (ClassNotFoundException ex) {
      error("Type class [" + defaultTypeName + "] not found for Map key/value type", entryEle, ex);
      return value;
    }
  }

  /**
   * Parse a key sub-element of a map element.
   */
  @Nullable
  protected Object parseKeyElement(Element keyEle, @Nullable BeanDefinition bd, String defaultKeyTypeName) {
    NodeList nl = keyEle.getChildNodes();
    Element subElement = null;
    for (int i = 0; i < nl.getLength(); i++) {
      Node node = nl.item(i);
      if (node instanceof Element) {
        // Child element is what we're looking for.
        if (subElement != null) {
          error("<key> element must not contain more than one value sub-element", keyEle);
        }
        else {
          subElement = (Element) node;
        }
      }
    }
    if (subElement == null) {
      return null;
    }
    return parsePropertySubElement(subElement, bd, defaultKeyTypeName);
  }

  /**
   * Parse a props element.
   */
  public Properties parsePropsElement(Element propsEle) {
    ManagedProperties props = new ManagedProperties();
    props.setSource(extractSource(propsEle));
    props.setMergeEnabled(parseMergeAttribute(propsEle));

    List<Element> propEles = DomUtils.getChildElementsByTagName(propsEle, PROP_ELEMENT);
    for (Element propEle : propEles) {
      String key = propEle.getAttribute(KEY_ATTRIBUTE);
      // Trim the text value to avoid unwanted whitespace
      // caused by typical XML formatting.
      String value = DomUtils.getTextValue(propEle).trim();
      TypedStringValue keyHolder = new TypedStringValue(key);
      keyHolder.setSource(extractSource(propEle));
      TypedStringValue valueHolder = new TypedStringValue(value);
      valueHolder.setSource(extractSource(propEle));
      props.put(keyHolder, valueHolder);
    }

    return props;
  }

  /**
   * Parse the merge attribute of a collection element, if any.
   */
  public boolean parseMergeAttribute(Element collectionElement) {
    String value = collectionElement.getAttribute(MERGE_ATTRIBUTE);
    if (isDefaultValue(value)) {
      value = this.defaults.getMerge();
    }
    return TRUE_VALUE.equals(value);
  }

  /**
   * Parse a custom element (outside of the default namespace).
   *
   * @param ele the element to parse
   * @return the resulting bean definition
   */
  @Nullable
  public BeanDefinition parseCustomElement(Element ele) {
    return parseCustomElement(ele, null);
  }

  /**
   * Parse a custom element (outside of the default namespace).
   *
   * @param ele the element to parse
   * @param containingBd the containing bean definition (if any)
   * @return the resulting bean definition
   */
  @Nullable
  public BeanDefinition parseCustomElement(Element ele, @Nullable BeanDefinition containingBd) {
    String namespaceUri = getNamespaceURI(ele);
    if (namespaceUri == null) {
      return null;
    }
    NamespaceHandler handler = this.readerContext.getNamespaceHandlerResolver().resolve(namespaceUri);
    if (handler == null) {
      error("Unable to locate NamespaceHandler for XML schema namespace [" + namespaceUri + "]", ele);
      return null;
    }
    return handler.parse(ele, new ParserContext(this.readerContext, this, containingBd));
  }

  /**
   * Decorate the given bean definition through a namespace handler, if applicable.
   *
   * @param ele the current element
   * @param originalDef the current bean definition
   * @return the decorated bean definition
   */
  public BeanDefinitionHolder decorateBeanDefinitionIfRequired(Element ele, BeanDefinitionHolder originalDef) {
    return decorateBeanDefinitionIfRequired(ele, originalDef, null);
  }

  /**
   * Decorate the given bean definition through a namespace handler, if applicable.
   *
   * @param ele the current element
   * @param originalDef the current bean definition
   * @param containingBd the containing bean definition (if any)
   * @return the decorated bean definition
   */
  public BeanDefinitionHolder decorateBeanDefinitionIfRequired(
          Element ele, BeanDefinitionHolder originalDef, @Nullable BeanDefinition containingBd) {

    BeanDefinitionHolder finalDefinition = originalDef;

    // Decorate based on custom attributes first.
    NamedNodeMap attributes = ele.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
      Node node = attributes.item(i);
      finalDefinition = decorateIfRequired(node, finalDefinition, containingBd);
    }

    // Decorate based on custom nested elements.
    NodeList children = ele.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
      Node node = children.item(i);
      if (node.getNodeType() == Node.ELEMENT_NODE) {
        finalDefinition = decorateIfRequired(node, finalDefinition, containingBd);
      }
    }
    return finalDefinition;
  }

  /**
   * Decorate the given bean definition through a namespace handler,
   * if applicable.
   *
   * @param node the current child node
   * @param originalDef the current bean definition
   * @param containingBd the containing bean definition (if any)
   * @return the decorated bean definition
   */
  public BeanDefinitionHolder decorateIfRequired(
          Node node, BeanDefinitionHolder originalDef, @Nullable BeanDefinition containingBd) {

    String namespaceUri = getNamespaceURI(node);
    if (namespaceUri != null && !isDefaultNamespace(namespaceUri)) {
      NamespaceHandler handler = readerContext.getNamespaceHandlerResolver().resolve(namespaceUri);
      if (handler != null) {
        BeanDefinitionHolder decorated =
                handler.decorate(node, originalDef, new ParserContext(readerContext, this, containingBd));
        if (decorated != null) {
          return decorated;
        }
      }
      else if (namespaceUri.startsWith("http://www.springframework.org/schema/")) {
        error("Unable to locate NamespaceHandler for XML schema namespace [" + namespaceUri + "]", node);
      }
      else {
        // A custom namespace, not to be handled by Framework - maybe "xml:...".
        if (log.isDebugEnabled()) {
          log.debug("No NamespaceHandler found for XML schema namespace [{}]", namespaceUri);
        }
      }
    }
    return originalDef;
  }

  @Nullable
  private BeanDefinitionHolder parseNestedCustomElement(Element ele, @Nullable BeanDefinition containingBd) {
    BeanDefinition innerDefinition = parseCustomElement(ele, containingBd);
    if (innerDefinition == null) {
      error("Incorrect usage of element '" + ele.getNodeName() + "' in a nested manner. " +
              "This tag cannot be used nested inside <property>.", ele);
      return null;
    }
    String id = ele.getNodeName() + BeanDefinitionReaderUtils.GENERATED_BEAN_NAME_SEPARATOR +
            ObjectUtils.getIdentityHexString(innerDefinition);
    if (log.isTraceEnabled()) {
      log.trace("Using generated bean name [{}] for nested custom element '{}'", id, ele.getNodeName());
    }
    return new BeanDefinitionHolder(innerDefinition, id);
  }

  /**
   * Get the namespace URI for the supplied node.
   * <p>The default implementation uses {@link Node#getNamespaceURI}.
   * Subclasses may override the default implementation to provide a
   * different namespace identification mechanism.
   *
   * @param node the node
   */
  @Nullable
  public String getNamespaceURI(Node node) {
    return node.getNamespaceURI();
  }

  /**
   * Get the local name for the supplied {@link Node}.
   * <p>The default implementation calls {@link Node#getLocalName}.
   * Subclasses may override the default implementation to provide a
   * different mechanism for getting the local name.
   *
   * @param node the {@code Node}
   */
  public String getLocalName(Node node) {
    return node.getLocalName();
  }

  /**
   * Determine whether the name of the supplied node is equal to the supplied name.
   * <p>The default implementation checks the supplied desired name against both
   * {@link Node#getNodeName()} and {@link Node#getLocalName()}.
   * <p>Subclasses may override the default implementation to provide a different
   * mechanism for comparing node names.
   *
   * @param node the node to compare
   * @param desiredName the name to check for
   */
  public boolean nodeNameEquals(Node node, String desiredName) {
    return desiredName.equals(node.getNodeName()) || desiredName.equals(getLocalName(node));
  }

  /**
   * Determine whether the given URI indicates the default namespace.
   */
  public boolean isDefaultNamespace(@Nullable String namespaceUri) {
    return StringUtils.isEmpty(namespaceUri) || BEANS_NAMESPACE_URI.equals(namespaceUri);
  }

  /**
   * Determine whether the given node indicates the default namespace.
   */
  public boolean isDefaultNamespace(Node node) {
    return isDefaultNamespace(getNamespaceURI(node));
  }

  private boolean isDefaultValue(String value) {
    return StringUtils.isEmpty(value) || DEFAULT_VALUE.equals(value);
  }

  private boolean isCandidateElement(Node node) {
    return (node instanceof Element && (isDefaultNamespace(node) || !isDefaultNamespace(node.getParentNode())));
  }

}
